package com.edu.robot.core.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.edu.chat.facade.facade.SessionFacade;
import com.edu.chat.facade.model.dto.SceneInfoDTO;
import com.edu.chat.facade.model.dto.SessionInfoDetailDTO;
import com.edu.robot.core.enums.IntentEnum;
import com.edu.robot.core.model.QueryContext;
import com.edu.robot.core.service.ModelManger;
import com.edu.robot.core.service.ModelStrategy;
import com.edu.robot.core.service.RobotService;
import com.edu.robot.model.R;
import com.edu.robot.model.enums.ResultCodeEnum;
import com.edu.robot.model.error.BizException;
import com.edu.robot.modules.im.controller.vo.MessageVO;
import com.edu.robot.modules.robot.dto.RobotConfigDTO;
import com.edu.robot.modules.robot.dto.RobotInfoDTO;
import com.edu.robot.modules.robot.service.CrRobotService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @program: KQAIA
 * @description:
 * @author: 无恙
 * @create: 2025-03-23 11:08
 **/

@Service
@Slf4j
public class RobotServiceImpl implements RobotService {

	@Autowired
	private ModelManger modelManger;

	@Autowired
	private CrRobotService crRobotService;

	@DubboReference(interfaceClass = SessionFacade.class,check = false)
	private SessionFacade sessionFacade;


	private final Map<String, ModelStrategy> modelStrategyMap=new ConcurrentHashMap<>(12);

	/**
	 * 机器人查询方法，根据查询上下文确定机器人的响应策略
	 *
	 * @param queryContext 查询上下文，包含用户查询、机器人信息和配置等
	 */
	@Override
	public void robotQuery(QueryContext queryContext) {

	    //调用模型,进行意图识别
	    String intent = modelManger.intentRecognition(queryContext);

	    //根据识别的意图类型获取对应的意图枚举
	    IntentEnum intentAgent = IntentEnum.getIntentAgentByType(intent);

	    //默认走闲聊
	    if(null == intentAgent){
	        intentAgent=IntentEnum.INTENT_CHAT;
	    }

	    //判断调用工具意图前置校验
	    if(IntentEnum.INTENT_TOOL.equals(intentAgent)){
	        RobotInfoDTO robotInfoDTO = queryContext.getRobotInfoDTO();

	        RobotConfigDTO robotConfig = robotInfoDTO.getRobotConfig();

	        List<String> toolConfigId = robotConfig.getToolConfigId();

	        //如果工具为null,默认闲聊意图
	        if(CollectionUtil.isEmpty(toolConfigId)) {
	            log.info("RobotServiceImpl robotQuery 该机器人没有配置工具！");
	            intentAgent=IntentEnum.INTENT_CHAT;
	        }
	    }

	    //根据意图获取对应的模型策略
	    ModelStrategy modelStrategy = modelStrategyMap.get(intentAgent.getCode());

	    //执行对应的模型策略处理方法
	    modelStrategy.process(queryContext);

	}


	@EventListener
	public void handleContextRefreshedEvent(ContextRefreshedEvent event) {
		//获取所有模型策略
		Map<String, ModelStrategy> beansOfType = event.getApplicationContext().getBeansOfType(ModelStrategy.class);
		log.info("获取所有模型策略:{}",beansOfType);
		modelStrategyMap.putAll(beansOfType);
	}
}
